/*
 * //Author: Karthic Kumar Sekar [A0092622]
 */

package sg.edu.nus.iss.team4ft.manager;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import sg.edu.nus.iss.team4ft.exception.CustomerAccountException;
import sg.edu.nus.iss.team4ft.service.CustomerAccount;
import sg.edu.nus.iss.team4ft.service.Plan;
import sg.edu.nus.iss.team4ft.util.AppConstant;
import sg.edu.nus.iss.team4ft.util.IOService;

public class CustomerAccountManager {
	
	private List<CustomerAccount> customerAccounts = new ArrayList<CustomerAccount>();
	
	public CustomerAccountManager() throws NullPointerException, IOException, ParseException, ClassNotFoundException {
		updateCustomerAccount();
	}
	
	public void registerCustomerAccount(CustomerAccount customerAccount, boolean isDeactive) throws Exception{
		List<CustomerAccount> removeCusAcc = new ArrayList<CustomerAccount>();		
		for (CustomerAccount ca : customerAccounts) {
			if(ca.getAccount().getAccountNumber().equals(customerAccount.getAccount().getAccountNumber()))
				removeCusAcc.add(ca);				
		}
		customerAccounts.removeAll(removeCusAcc);	
		
		if(this.getCustomerAccountFromNRIC(customerAccount.getCustomer().getNRIC()) != null)
			throw new CustomerAccountException("A customer with same NRIC exist!");
		
		if(!isDeactive){
			activateCustomerAccount(customerAccount);
			boolean hasValidPlan = false;
			for (Plan p : customerAccount.getAccount().getPlans()) {
				if(p.getEndDate() == null)
					hasValidPlan = true;
			}
			if(!hasValidPlan)
				throw new CustomerAccountException("At least one plan must be subscribed!");
		}
		
		customerAccounts.add(customerAccount);
		IOService.saveObjToFile(getAllCustomerAccount(), AppConstant.CUSTOMER_ACCOUNT_FILE_NAME);		
	}
	
	@SuppressWarnings("unchecked")
	public void updateCustomerAccount() throws IOException, ClassNotFoundException{
		Object o = IOService.readObjFromFile(AppConstant.CUSTOMER_ACCOUNT_FILE_NAME);
		if(o != null)
			customerAccounts = (ArrayList<CustomerAccount>)(o);
	}
	
	public boolean deregisterCustomerAccount(String NRIC) throws Exception{
		try
		{
			CustomerAccount ca = getCustomerAccountFromNRIC(NRIC);
			deregisterCustomerAccount(ca);
			registerCustomerAccount(ca, true);
		}
		catch (Exception e) {
			throw e;
		}
		return true;
	}
	
	public CustomerAccount getCustomerAccountFromNRIC(String NRIC) throws CloneNotSupportedException{
		CustomerAccount ca = null;
		for (CustomerAccount t : customerAccounts) {
			if(t.getCustomer().NRICEquals(NRIC))
			{
				ca = t;
				break;
			}			
		}
		return ca;
	}
	
	public CustomerAccount getCustomerAccountFromAccountNum(String accountNumber) throws CloneNotSupportedException{
		CustomerAccount ca = null;
		for (CustomerAccount t : customerAccounts) {
			if(t.getAccount().accountNumberEquals(accountNumber))
			{
				ca = t;
				break;
			}			
		}
		return ca;
	}
	
	public ArrayList<CustomerAccount> getAllCustomerAccount(){		
		return new ArrayList<CustomerAccount>(customerAccounts);
	}
	
	private void deregisterCustomerAccount(CustomerAccount ca) throws Exception{
		ca.deregisterCustomer();
	}
	
	private void activateCustomerAccount(CustomerAccount ca) throws Exception{
		ca.activateCustomer();
	}
}
